package com.tangmu.rabbits.ui.fragment.home;

import android.util.Log;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ListView;

import com.tangmu.rabbits.R;
import com.tangmu.rabbits.adapter.home.HomeGridAdapter;
import com.tangmu.rabbits.adapter.home.LvAdapter;
import com.tangmu.rabbits.base.BaseOneFragment;
import com.tangmu.rabbits.ui.activity.home.BrandIflashbuyActivity;
import com.tangmu.rabbits.ui.activity.home.NineElementActivity;
import com.tangmu.rabbits.ui.activity.home.TodayChooseActivity;
import com.tangmu.rabbits.util.ChenColorUtils;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * 首页
 */
public class HomeFragment extends BaseOneFragment {

    public static final String TAG = "HomeFragment";

    @BindView(R.id.home_list)
    ListView listView;
    /* @BindView(R.id.invis)
     LinearLayout invis;*/
    @BindView(R.id.home_toolbar)
    View titleBar;
    View headerView;
    View hideView;
    View realTab;
    GridView gridView;
    private HomeGridAdapter gridAdapter;
    private List<Integer> datas = new ArrayList<>();
    List<String> strs = new ArrayList<>();

    //listview的头部（这里是ImageView）顶部距离屏幕顶部（包含状态栏）的距离
    //注：这个高度，是头布局在屏幕里才会计算的，出了屏幕，就不会变了
    private int height;
    //listView的头部的真实高度。头布局的整体高度，因为这个demo只简单写了个ImageView作为头部，所以ImageView的高度，就是头部的高度
    private int headViewHeight;

    public HomeFragment() {
        // Required empty public constructor
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        mImmersionBar.statusBarColor(R.color.transparent).titleBar(R.id.home_toolbar).init();
    }

    @Override
    protected int getLayoutResource() {
        return R.layout.fragment_multi_home;
    }

    @Override
    protected void initView() {
        /*for (int i = 0; i < 4; i++) {
            datas.add(i);
        }*/
        datas.add(R.drawable.icon_today_biqiang);
        datas.add(R.drawable.icon_nine_to_nine);
        datas.add(R.drawable.icon_brand_iflash);
        datas.add(R.drawable.icon_yuan20);

        for (int i = 0; i < 20; i++) {
            strs.add("【阿郎说】男士纯棉平角内裤4条礼盒送袜子" + i);
        }
        headerView = getLayoutInflater().inflate(R.layout.include_home_content, null);
        hideView = mActivity.findViewById(R.id.layout_tabs);
        realTab = headerView.findViewById(R.id.real_tab);
        gridView = headerView.findViewById(R.id.home_gridView);

        gridAdapter = new HomeGridAdapter(mActivity, datas);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (position == 0) {
                    startActivity(NineElementActivity.class);
                } else if (position == 1) {
                    startActivity(TodayChooseActivity.class);
                } else if (position == 2) {
                    startActivity(BrandIflashbuyActivity.class);
                } else if (position == 3) {
                    startActivity(NineElementActivity.class);
                }
            }
        });

        listView.addHeaderView(headerView);//ListView条目中的悬浮部分 添加到头部
        LvAdapter adapter = new LvAdapter(mActivity, strs);
        listView.setAdapter(adapter);
        listView.setOnScrollListener(new AbsListView.OnScrollListener() {

            @Override
            public void onScrollStateChanged(AbsListView view, int scrollState) {

            }

            @Override
            public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {

                // 处理标题栏颜色渐变
                int location[] = new int[2];
                headerView.getLocationInWindow(location);
                //listview的头部（这里是ImageView）顶部距离屏幕顶部（包含状态栏）的距离
                height = location[1];
                headViewHeight = headerView.getHeight();

                Log.i(TAG, "==location==0==" + location[0]);
                Log.i(TAG, "==location==1==" + location[1]);
                Log.i(TAG, "==height==" + height);
                Log.i(TAG, "==headViewHeight==" + headViewHeight);
                handleTitleBarColorEvaluate();
                if (firstVisibleItem >= 1) {
                    titleBar.setBackgroundResource(R.drawable.shape_immersion);
                }
            }

        });

    }

    // 处理标题栏颜色渐变
    private void handleTitleBarColorEvaluate() {
        //比例
        float fraction;
        if (height > 0) {

            fraction = 1f - height * 1f / 60;
            if (fraction < 0f) {
                fraction = 0f;
            }
            titleBar.setAlpha(fraction);
            return;
        }

        //高度值是负数，但是负号仅仅是表示方向，取绝对值
        float space = Math.abs(height) * 1f;
        // 标题栏的高度
        fraction = space / headViewHeight;
        if (fraction < 0f)
            fraction = 0f;
        if (fraction > 1f)
            fraction = 1f;
        titleBar.setAlpha(1f);

        if (fraction >= 1f) {
            titleBar.setBackgroundColor(mActivity.getResources().getColor(R.color.colorPrimaryDark));
        } else {
            //根据比例，生成一个按比例的颜色值
            //根据比例，生成一个按比例的颜色值
            titleBar.setBackgroundColor(ChenColorUtils.getNewColorByStartEndColor(mActivity, fraction, R.color.transparent, R.color.colorPrimaryDark));
        }
       /* if (fraction >= 0.8f) {
            title_tv.setTextColor(ChenColorUtils.getNewColorByStartEndColor(context, fraction, R.color.transparent, R.color.black));
            title_tv.setText("标题栏");
        } else {
            title_tv.setText("");
        }*/
    }

}
